package com.hmall.cart.listener;

import com.hmall.cart.service.impl.CartServiceImpl;
import com.hmall.common.config.MyBatisConfig;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.utils.UserContext;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author wangyoucheng
 * @date 2025/9/4
 * @description TOD0
 */
@Component
public class CartClearListener {

    @Autowired
    private CartServiceImpl cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstants.TRADE_SUCCESS_QUEUE, durable = "true"),
            exchange = @Exchange(value = MqConstants.TRADE_EXCHANGE_NAME, type = ExchangeTypes.DIRECT),
            key = MqConstants.TRADE_SUCCESS_KEY
    ))
    public void listenerCartSuccess(Map<String, Object> carts) {
        List<Long> itemids = (List<Long>) carts.get("itemId");
        UserContext.setUser((Long) carts.get("userId"));
        cartService.removeByItemIds(itemids);

    }
}
